package com.baor.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss";
    /**
     * now() -> yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String now(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public static String now2(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    /**
     * <-- 向下取整 -->
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 毫秒差值
     */
    public static Long getSubtractMillis(String startTime,String endTime)  {
        if ( isAfter(startTime,endTime) ){
            String temp = startTime;
            startTime = endTime;
            endTime = temp;
        }
        Calendar start = stringToCalendar(startTime);
        Calendar end = stringToCalendar(endTime);
        long subtractMillis = end.getTimeInMillis() - start.getTimeInMillis();//毫秒差
        return subtractMillis;
    }

    /**
     * @return 秒差值
     */
    public static Long getSubtractSecs(String startTime,String endTime){
        long subtractMillis = getSubtractMillis(startTime,endTime);
        long subtractSecs = subtractMillis / 1000;
        return subtractSecs;
    }

    /**
     * @return 分钟差值
     */
    public static Long getSubtractMins(String startTime,String endTime){
        long subtractSecs = getSubtractSecs(startTime,endTime);
        long subtractMins = subtractSecs / 60;
        return subtractMins;
    }

    /**
     * @return 小时差值
     */
    public static Long getSubtractHours(String startTime,String endTime){
        long subtractMins = getSubtractMins(startTime,endTime);
        long subtractHours = subtractMins / 60;
        return subtractHours;
    }

    /**
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 天数差值
     */
    public static Long getSubtractDays(String startTime,String endTime){
        long subtractHours = getSubtractHours(startTime,endTime);
        long subtractDays = subtractHours / 24;
        return subtractDays;
    }

//    public static void main(String[] args) {
//        String startTime = "2020-01-01 00:00:00";
//        String endTime = "2020-01-02 00:01:01";
//        System.out.println(getSubtractMillis(startTime,endTime));
//        System.out.println(getSubtractSecs(startTime,endTime));
//        System.out.println(getSubtractMins(startTime,endTime));
//        System.out.println(getSubtractHours(startTime,endTime));
//        System.out.println(getSubtractDays(startTime,endTime));
//        System.out.println(isAfter(startTime,endTime));
//        System.out.println(isBefore(startTime,endTime));
//    }

    /**
     * String -> Date
     * @param str
     * @return
     */
    public static Date parseStrToDate(String str){
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Date parse = null;
        try {
            parse = sdf.parse(str);
        } catch (ParseException e) {
            System.out.println("DateUtil异常:{}");
            throw new RuntimeException(e);
        }
        return parse;
    }

    /**
     * String -> Calendar
     * @param strTime
     * @return
     */
    public static Calendar stringToCalendar(String strTime){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseStrToDate(strTime));
        return calendar;
    }

    /**
     * start 后于 end
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isAfter(String startTime,String endTime){
        Calendar start = stringToCalendar(startTime);
        Calendar end = stringToCalendar(endTime);
        return start.compareTo(end) > 0;
    }

    /**
     * start 先于 end
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isBefore(String startTime,String endTime){
        Calendar start = stringToCalendar(startTime);
        Calendar end = stringToCalendar(endTime);
        return start.compareTo(end) < 0;
    }

    //获取下个月第一天
    public static String getPerFirstDayOfMonth() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return simpleDateFormat.format(calendar.getTime());
    }

    //获取下个月第一天
    public static String getPerFirstDayOfNextQuarter() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        int currentMonth = calendar.get(Calendar.MONTH);
        int currentQuarter = (currentMonth / 3) + 1;
        int nextQuarter = currentQuarter < 4 ? currentQuarter + 1 : 1;
        int monthToSet = (nextQuarter - 1) * 3; // 计算下一个季度的第一个月
        calendar.add(Calendar.MONTH, monthToSet - currentMonth); // 设置为下一个季度的第一个月
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); // 设置为当月的第一天
        return simpleDateFormat.format(calendar.getTime());
    }


}
